/**
 * Created by Vladimir Falcon
 */


jQuery(document).ready(function(){

    /**
     * Recalculate the width of all Tabs
     * only if tabs container > children Total width
     */

    if (jQuery('ul.ui-tabs-nav').length != 0){
        jQuery('ul.ui-tabs-nav').each(function(index, item) {
            var childs = jQuery(this).children('li');
            var parentWidth = jQuery(this).outerWidth(true);
            var childrenTotalWidth = 0;

            childs.each(function(cIndex,item){
                var child = jQuery(item);
                childrenTotalWidth += child.outerWidth(true);
            });

            if (parentWidth > childrenTotalWidth){
                var restWidth = (parentWidth - childrenTotalWidth ) / childs.length;

                childs.each(function(cIndex,item){
                    var child = jQuery(item);
                    child.css('width',"+="+restWidth);
                });
            }
        });

        // Fix tab height

        jQuery('div.tx-jfmulticontent-pi1 div.ui-tabs').each(function(index, item) {
            var childs = jQuery(this).children('div.ui-tabs-panel');

            var maxHeight = 0;
            childs.each(function(cIndex,citem){
                var child = jQuery(citem);
                if (maxHeight < child.outerHeight(true)){
                    maxHeight = child.outerHeight(true);
                }

				//console.log('maxHeight: ' + maxHeight);
				//console.log('outerHeight: ' + child.outerHeight(true));

            });
            if (maxHeight > 0){
                childs.height(maxHeight);
            }

        });
    }
    


    /**
     * Animation home page only
     */
    if (jQuery('div#bannerAnimation').length != 0){

        var headerBannerData = [];
        var from = ['top right 1.5x','top left 1.5x','bottom left 1.5x'];
        var to = ['bottom left 1x','bottom right 1x','top right 1x'];
        jQuery('ul#bannernavi li').each(function(index, item) {
            headerBannerData.push({
                src:jQuery('img.image', this).attr('src'),
                alt:jQuery('span.bannerText', this).text(),
                href:jQuery('a', this).attr('href'),
                target:jQuery('a', this).attr('target'),
                from:from[index],
                to:to[index],
                time:10
            });

        });


        $('#bannerAnimation').crossSlide({
            fade:1,
            variant: true,
            easing: 'easeInOutQuad'
        }, headerBannerData, function (idx, img, idxOut, imgOut) {
            if (idxOut == undefined) {
                var captionText = img.alt;
                captionText = captionText.split("|");
                jQuery('div.caption p.captionTitle').text(captionText[0]);
                jQuery('div.caption p.captionText').text(captionText[1]);
                jQuery('div.caption').animate({ opacity:1 });
                // Active states
                var current = idx + 1;
                for (var i=1;i<=3;i++){
                    if(current == i){
                        $('li.bannernavi_'+i).animate({ opacity:1 });
                    }else{
                        $('li.bannernavi_'+i).animate({ opacity:.7 });
                    }
                }

            } else {
                var captionText = img.alt;
                jQuery('div.caption').animate({ opacity:0 });
                captionText = captionText.split("|");
                jQuery('div.caption p.captionTitle').text(captionText[0]);
                jQuery('div.caption p.captionText').text(captionText[1]);
            }
        });
    }

    if (jQuery('div#search').length != 0){
        var mainSearch = jQuery('div#search input.tx-solr-q');
        var pageLanguage = jQuery('div#search input[name=L]').val();
        var mainSearchLabels = ["Full-text search","Volltextsuche","Recherche en texte intÃ©gral","Wyszukiwanie peÅ‚notekstowe"];

        mainSearch.click(function() {
            if (jQuery.inArray(mainSearch.val(),mainSearchLabels) > -1){
                mainSearch.val('');
            }
        });

        mainSearch.keyup(function() {
            if (mainSearch.val() == ''){
                mainSearch.val(mainSearchLabels[pageLanguage]).select();
            }
        });

        if (mainSearch.val() == ''){
            mainSearch.val(mainSearchLabels[pageLanguage]);
        }
    }



});